﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AmiEmuLib.AmigaMachine.AmigaMemory;
using System.Diagnostics;

namespace AmiEmuLib.AmigaMachine.AmigaCPU
{
    internal class CPU68020 : CPU680x0
    {
        private uint m_cacr;

        /// <summary>
        /// Prefetch.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(IR)}")]
        public uint IR;

        /// <summary>
        /// Prefetch.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(IRC)}")]
        public uint IRC;

        /// <summary>
        /// Master stack pointer.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(MSP)}")]
        public uint MSP;

        /// <summary>
        /// Interrupt stack pointer.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(ISP)}")]
        public uint ISP;

        /// <summary>
        /// User stack pointer.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(USP)}")]
        public uint USP;

        /// <summary>
        /// Destination function code.
        /// </summary>
        public byte DFC;

        /// <summary>
        /// Source function code.
        /// </summary>
        public byte SFC;

        /// <summary>
        /// Vectors base address.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(VBR)}")]
        public uint VBR;

        /// <summary>
        /// Cache control register.
        /// </summary>
        [DebuggerDisplay("{AmiEmuLib.AmigaMachine.AmigaCPU.CACRRegistry.Info(CACR)}")]
        public uint CACR
        {
            get
            {
                return m_cacr;
            }
            set
            {
                m_cacr = value & (CACRRegister.MASK_ENABLED | CACRRegister.MASK_FROZEN);
            }
        }

        /// <summary>
        /// Cache address register.
        /// </summary>
        [DebuggerDisplay("{Hex.UIntToHex(CAAR)}")]
        public uint CAAR;

        /// <summary>
        /// Status register.
        /// </summary>
        [DebuggerDisplay("{AmiEmuLib.AmigaMachine.AmigaCPU.SR68020.Info(SR)}, {AmiEmuLib.AmigaMachine.AmigaCPU.CCRRegistry.Info(SR)}")]
        public ushort SR;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="a_machine"></param>
        internal CPU68020(Machine a_machine) 
            : base(a_machine)
        {
        }

        protected override void Reset()
        {
            base.Reset();

            IR = 0;
            IRC = 0;

            MSP = 0;
            ISP = 0;
            USP = 0;

            DFC = 0;
            SFC = 0;

            VBR = 0;

            CAAR = 0;
            CACR = 0;

            SR = 0;
            SR68000.SetSupervisor(ref SR, true);
        }
    }
}